<html>
 <head>
  <link href="./leetcode-problem.css" rel="stylesheet" type="text/css">
 </head>
 <body>
  <div class="question_difficulty">
   难度：Medium
  </div>
  <div>
   <h1 class="question_title">
    230. Kth Smallest Element in a BST
   </h1>
   <p>
    Given a binary search tree, write a function
    <code>
     kthSmallest
    </code>
    to find the
    <b>
     k
    </b>
    th smallest element in it.
   </p>
   <p>
    <b>
     Note:
    </b>
    <br>
    You may assume k is always valid, 1 &le; k &le; BST's total elements.
   </p>
   <p>
    <strong>
     Example 1:
    </strong>
   </p>
   <pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
&nbsp;  2
<strong>Output:</strong> 1</pre>
   <p>
    <strong>
     Example 2:
    </strong>
   </p>
   <pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
<strong>Output:</strong> 3
</pre>
   <p>
    <b>
     Follow up:
    </b>
    <br>
    What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
   </p>
  </div>
  <div>
   <h1 class="question_title">
    230. 二叉搜索树中第K小的元素
   </h1>
   <p>
    给定一个二叉搜索树，编写一个函数&nbsp;
    <code>
     kthSmallest
    </code>
    &nbsp;来查找其中第&nbsp;
    <strong>
     k&nbsp;
    </strong>
    个最小的元素。
   </p>
   <p>
    <strong>
     说明：
    </strong>
    <br>
    你可以假设 k 总是有效的，1 &le; k &le; 二叉搜索树元素个数。
   </p>
   <p>
    <strong>
     示例 1:
    </strong>
   </p>
   <pre><strong>输入:</strong> root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
&nbsp;  2
<strong>输出:</strong> 1</pre>
   <p>
    <strong>
     示例 2:
    </strong>
   </p>
   <pre><strong>输入:</strong> root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
<strong>输出:</strong> 3</pre>
   <p>
    <strong>
     进阶：
    </strong>
    <br>
    如果二叉搜索树经常被修改（插入/删除操作）并且你需要频繁地查找第 k 小的值，你将如何优化&nbsp;
    <code>
     kthSmallest
    </code>
    &nbsp;函数？
   </p>
  </div>
 </body>
</html>